/*
 * DemoChannelListener.java
 *
 * Research In Motion Limited proprietary and confidential
 * Copyright Research In Motion Limited, 2011-2011
 */
package com.allkapps.bbmopenchat;

import com.allkapps.bbmopenchat.BaseScreen;

import net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnection;
import net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnectionListener;
import net.rim.blackberry.api.bbm.platform.io.BBMPlatformData;
import net.rim.blackberry.api.bbm.platform.io.BBMPlatformIncomingJoinRequest;
import net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContact;
import net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContactList;
import net.rim.blackberry.api.bbm.platform.ui.chat.component.ChatField;

/**
 * Responds to connection events and asks the view components to update themselves accordingly.
 * *LOOK_HERE*
 * <p>
 * Recall that Connection Listeners are required whenever a connection is {@link {@link BBMBridge#createChannel(ContactListField, ChatField) created}.
 * <p>
 * You should notice that {@link BBMOpenChatChannelListener} and {@link BBMOpenChatSessionListener}
 * use this class. Channel and Session listeners both implement a
 * {@link BBMPlatformConnectionListener}. We can leverage this inheritance property
 * to have a common set of logic. You will also notice a similar pattern on
 * the view/UI.
 *
 * @see {@link ConnectionScreen}
 * @see {@link BBMBridge#createChannel(com.rim.samples.device.bbm.sdkdemo.view.ChannelScreen)}
 * @see {@link BBMOpenChatChannelListener}
 * @see {@link BBMOpenChatSessionListener}
 * @see {@link net.rim.blackberry.api.bbm.platform.service.MessagingService}
 */
class BBMOpenChatConnectionListener implements BBMPlatformConnectionListener {

	private BaseScreen screen = null;
	
    BBMOpenChatConnectionListener() {
    }
    
    public void setScreen(BaseScreen screen) {
    	this.screen = screen;
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnectionListener#contactDeclined(net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnection, net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContact)
     */
    public void contactDeclined(BBMPlatformConnection connection,
            BBMPlatformContact contact) {

    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnectionListener#contactLeft(net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnection, net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContact)
     */
    public void contactLeft(BBMPlatformConnection connection, BBMPlatformContact contact) {
    	if(screen != null){
    		screen.updateScreen(BaseScreen.CONTACT_LEFT, contact);
    	}
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnectionListener#contactsInvited(net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnection, net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContactList)
     */
    public void contactsInvited(BBMPlatformConnection connection, BBMPlatformContactList contactList) {
    	if(screen != null){
    		screen.updateScreen(BaseScreen.CONTACT_INVITED, contactList);
    	}
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnectionListener#contactsJoined(net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnection, net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContactList, java.lang.String, int)
     */
    public void contactsJoined(BBMPlatformConnection connection, BBMPlatformContactList contactList, String cookie, int type) {
    	//Do something when contacts join like add a message to the screen
    	if(screen != null){
    		screen.updateScreen(BaseScreen.CONTACT_JOINED, contactList);
    	}
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnectionListener#dataReceived(net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnection, net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContact, net.rim.blackberry.api.bbm.platform.io.BBMPlatformData)
     */
    public void dataReceived(BBMPlatformConnection connection, BBMPlatformContact sender, BBMPlatformData data) {
    	if(screen != null){
    		screen.updateScreen(BaseScreen.MESSAGE_RECEIVED, sender, data);
    	}
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnectionListener#joinRequestCanceled(net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnection, net.rim.blackberry.api.bbm.platform.io.BBMPlatformIncomingJoinRequest, int)
     */
    public void joinRequestCanceled( BBMPlatformConnection platformConnection, BBMPlatformIncomingJoinRequest joinRequest, int reasonCode ) {
        // There is nothing to do. The joiner cancelled the request
        // to join to public connection.
    	if(screen != null){
    		screen.updateScreen(BaseScreen.JOIN_REQUEST_CANCEL, joinRequest, new Integer(reasonCode));
    	}
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnectionListener#joinRequestReceived(net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnection, net.rim.blackberry.api.bbm.platform.io.BBMPlatformIncomingJoinRequest, java.lang.String)
     */
    public void joinRequestReceived( BBMPlatformConnection platformConnection, BBMPlatformIncomingJoinRequest joinRequest, String cookie ) {
    	//Do whatever you want to do here. We are automatically accepting the request.
    	joinRequest.accept(cookie);
    }

    /**
     * Sends an update to the connection screen about administrative details
     * of the connection.
     *
     * @param message non null message
     *
     * @throws NullPointerException if message is null
     */
    protected void connectionUpdate(String contactName, int resourceId) {
    }

    /**
     * Sends an update to the connection screen about administrative details
     * of the connection.
     *
     * @param message non null message
     *
     * @throws NullPointerException if message is null
     */
    protected void connectionUpdate(String message) {
    }

}
